﻿using nj4x;
using nj4x.Metatrader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MT4
{
    public partial class Form1 : Form
    {

    List<Account> accounts;

     

        public Form1()
        {
            InitializeComponent();

            accounts = FileManager.FileManager.Deserialize<List<Account>>("accounts");

            string server = "127.0.0.1";
            int port = 7788;
            //generateTestAccounts();

            
            
            foreach (Account a in accounts)
            {
                a.Connect();
                ListViewItem broker = new ListViewItem(a.Broker);
                broker.SubItems.Add(a.Login);
                broker.SubItems.Add(a.Strategy().AccountBalance().ToString());
                broker.SubItems.Add(a.Strategy().AccountEquity().ToString());
                broker.SubItems.Add(a.Strategy().AccountCurrency());
                broker.Tag = a;
                brokers.Items.Add(broker);


                foreach (var trade in a.getOpenTrades()) 
                {
                    ListViewItem tradeItem = new ListViewItem(a.Broker);
                    tradeItem.SubItems.Add(a.Login);
                    tradeItem.SubItems.Add(trade.GetTicket().ToString());
                    tradeItem.SubItems.Add(trade.GetSymbol().ToString());
                    tradeItem.SubItems.Add(trade.GetTradeOperation().ToString());
                    tradeItem.SubItems.Add(trade.GetTakeProfit().ToString());
                    tradeItem.SubItems.Add(trade.GetStopLoss().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenPrice().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenTime().ToString());
                    tradeItem.SubItems.Add(trade.GetProfit().ToString());
                    tradeItem.SubItems.Add(a.Strategy().AccountCurrency());
                    tradeItem.SubItems.Add(trade.GetMagic().ToString());

                    tradeItem.Tag = a;

                    openTradesListView.Items.Add(tradeItem);        

                }

            }

            equityRefresher.Enabled = true;
            tradesRefresher.Enabled = true;
        }


      

      /*  private void generateTestAccounts()
        {
            List<Account> accounts = new List<Account>();

            accounts.Add(new Account("22096041",  "7anwzlx" , "GO4X-Demo"));
            accounts.Add(new Account("100269",  "0lfijdm" ,"NobleSecurities-Demo"));

            FileManager.FileManager.Serialize<List<Account>>("accounts", accounts);
        }*/

        private void addAccountToList()
        {
            List<Account> accounts = FileManager.FileManager.Deserialize<List<Account>>("accounts");

        }

        private void equityRefresher_Tick(object sender, EventArgs e)
        {
            foreach(ListViewItem item in brokers.Items) 
            {
                if (item.Tag is Account) 
                    {
                    Account acc = (Account)item.Tag;
                    item.SubItems[2].Text = acc.Strategy().AccountBalance().ToString();
                    item.SubItems[3].Text = acc.Strategy().AccountEquity().ToString();
                    }
            }

            

        }

        private void tradesRefresher_Tick(object sender, EventArgs e)
        {
            //Console.WriteLine("timer aktywny");
            
            // dodawanie openów
            foreach (Account a in accounts)
            {
                foreach (var trade in a.getOpenTrades())
                {
                    

                    ListViewItem tradeItem = new ListViewItem(a.Broker);
                    tradeItem.SubItems.Add(a.Login);
                    tradeItem.SubItems.Add(trade.GetTicket().ToString());
                    tradeItem.SubItems.Add(trade.GetSymbol().ToString());
                    tradeItem.SubItems.Add(trade.GetTradeOperation().ToString());
                    tradeItem.SubItems.Add(trade.GetTakeProfit().ToString());
                    tradeItem.SubItems.Add(trade.GetStopLoss().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenPrice().ToString());
                    tradeItem.SubItems.Add(trade.GetOpenTime().ToString());
                    tradeItem.SubItems.Add(trade.GetProfit().ToString());
                    tradeItem.SubItems.Add(a.Strategy().AccountCurrency());
                    tradeItem.SubItems.Add(trade.GetMagic().ToString());

                    tradeItem.Tag = a;

                    openTradesListView.Items.Add(tradeItem);

                }
            }


            //
            List<ListViewItem> toRemove = new List<ListViewItem>();

            foreach(ListViewItem item in openTradesListView.Items)
            {
                if (item.Tag is Account)
                {
                    Account account = (Account)item.Tag;
                    IOrderInfo order = account.Strategy().OrderGet(int.Parse(item.SubItems[2].Text), SelectionType.SELECT_BY_TICKET, SelectionPool.MODE_HISTORY);
                    {
                        if (order.GetCloseTime().CompareTo(new DateTime(1970, 1, 1)) > 0)
                        {
                            toRemove.Add(item);
                        }
                        else
                        {
                            item.SubItems[5].Text = order.GetTakeProfit().ToString();
                            item.SubItems[6].Text = order.GetStopLoss().ToString();
                            item.SubItems[9].Text = order.GetProfit().ToString();
                        }


                    }

                }

            }

            foreach (ListViewItem item in toRemove) 
            {
                openTradesListView.Items.Remove(item);
            }

        }

        private void addAccount_Click(object sender, EventArgs e)
        {
            string acc = textLogin.Text;
            string pass = textPass.Text;
            string server = textServer.Text;

           // Console.WriteLine("AC: "+ acc + " | Pass: " +pass);

            accounts.Add(new Account(acc, pass, server));
            FileManager.FileManager.Serialize<List<Account>>("accounts", accounts);

        }


      

    }
}
